package com.google.android.location.content;


import com.google.android.locatiom.maps.myroutes.R;
import com.google.android.location.content.libdata.MyRouteProvider;


import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class DeleteAllRoutes extends Handler {

	  private final Context context;
	  private final Runnable done;

	  public DeleteAllRoutes(Context context, Runnable done) {
	    this.context = context;
	    this.done = done;
	  }

	  @Override
	  public void handleMessage(Message msg) {
	    super.handleMessage(msg);

	    AlertDialog dialog = null;
	    AlertDialog.Builder builder = new AlertDialog.Builder(context);
	    builder.setMessage(
	        context.getString(R.string.route_list_delete_all_confirm_message));
	    builder.setTitle(context.getString(R.string.generic_confirm_title));
	    builder.setIcon(android.R.drawable.ic_dialog_alert);
	    builder.setPositiveButton(context.getString(R.string.generic_yes),
	        new DialogInterface.OnClickListener() {
	          public void onClick(DialogInterface dialogInterface, int i) {
	            dialogInterface.dismiss();
	            Log.w(Constants.TAG, "deleting all!");
	            MyRouteProvider.Factory.getRouteProvider(context).deleteAllRoutes();
	            SharedPreferences prefs = context.getSharedPreferences(
	                Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
	            Editor editor = prefs.edit();
	            // TODO: Go through data manager
	            editor.putLong(context.getString(R.string.selected_route_key), -1);
	            editor.commit();
	            if (done != null) {
	              Handler h = new Handler();
	              h.post(done);
	            }
	          }
	        });
	    builder.setNegativeButton(context.getString(R.string.generic_no),
	        new DialogInterface.OnClickListener() {
	          public void onClick(DialogInterface dialogInterface, int i) {
	            dialogInterface.dismiss();
	          }
	        });
	    dialog = builder.create();
	    dialog.show();
	  }
	}
